#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    // 1.主窗口
    this->setWindowTitle("智能家居设备管理系统");
    this->setWindowIcon(QIcon("://image/home.png")); // 更改标题栏图标
    this->resize(800, 700);

    // 2.主布局
    QTabWidget *tab_widget = new QTabWidget(this);
    SmartWidget *smart_widget = new SmartWidget;
    CameraWidget *camera_widget = new CameraWidget;
    MusicWidget *music_widget = new MusicWidget;
    tab_widget->addTab(smart_widget, "智能家居模块");
    tab_widget->addTab(camera_widget, "摄像头模块");
    tab_widget->addTab(music_widget, "音乐模块");

    // 3.中心组件
    this->setCentralWidget(tab_widget);

    // 4.状态栏
    // 4.1 获取原有的空白状态栏
    QStatusBar *status_bar = this->statusBar();
    // 4.2 创建状态栏显示标签
    statusBar_label = new QLabel("未连接");
    QPalette palette;
    palette.setColor(QPalette::WindowText, Qt::red);
    statusBar_label->setPalette(palette);
    // 4.3 将标签加载到状态栏
    status_bar->addWidget(statusBar_label);

    // 5.创建服务器
    tcpServer = tcpServerSingleton::create_tcpServer();

    // 6.信号/槽函数
    this->connect(tcpServer, &tcpServerSingleton::sig_recv, this, [this](const QString &recv){
        if(tcpServer->get_socket()->state() != QAbstractSocket::ConnectedState)
        {
            statusBar_label->setText("未连接");
            QPalette palette;
            palette.setColor(QPalette::WindowText, Qt::red);
            statusBar_label->setPalette(palette);
        }
        else
        {
            QString text = QString("已连接") + " " + recv;
            statusBar_label->setText(text);
            QPalette palette;
            palette.setColor(QPalette::WindowText, Qt::blue);
            statusBar_label->setPalette(palette);
        }
    });

    this->connect(smart_widget, &SmartWidget::sig_send, tcpServer, &tcpServerSingleton::slot_send_data);
    this->connect(camera_widget, &CameraWidget::sig_send, tcpServer, &tcpServerSingleton::slot_send_data);
    this->connect(music_widget, &MusicWidget::sig_send, tcpServer, &tcpServerSingleton::slot_send_data);
}

MainWindow::~MainWindow()
{

}
